package wc.Wc;

import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.ProcessFunction;
import org.apache.flink.streaming.api.functions.source.SocketTextStreamFunction;
import org.apache.flink.util.Collector;

public class Wc {

    public static void main(String[] args) throws Exception {

        Configuration flinkConf = new Configuration();
        flinkConf.setString("rest.port","9091");
        final StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(flinkConf);
        DataStreamSource<String> source = env.addSource(new SocketTextStreamFunction("localhost", 9999, "\n", -1));


        SingleOutputStreamOperator<String> ssos = source.flatMap(new FlatMapFunction<String, String>() {
            @Override
            public void flatMap(String value, Collector<String> out) throws Exception {
                for (String s : value.split(",")) {
                    System.out.println(out);
                    out.collect(s);
                }
            }
        }).setParallelism(2);

        ssos.map(new MapFunction<String, String>() {
            @Override
            public String map(String value) throws Exception {
                return value + "_heihei";
            }
        }).setParallelism(3).process(new ProcessFunction<String, String>() {
            @Override
            public void processElement(String value, ProcessFunction<String, String>.Context ctx, Collector<String> out) throws Exception {
                System.out.println(out);
                out.collect(value);
            }
        }).


                print().setParallelism(1);




        env.execute("app");
    }
}
